<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/shou' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>文章管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="文章名称">
					<el-input v-model="queryInfo.articleName" clearable @clear="queryList" placeholder="请输
入文章名称"></el-input>
				</el-form-item>

				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="addArticle">新增文章</el-button>
					<el-button type="danger">批量删除</el-button>
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 增加区域 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible">
			<el-form :model="articleData">
				<el-form-item label="文章名称" label-width="80px">
					<el-input placeholder="请输入文章名称" v-model="articleData.articleName" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="文章简介" label-width="80px">
					<el-input v-model="articleData.articleIntroduce" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="列表图" label-width="80px">
					<el-input placeholder="请输入列表图" v-model="articleData.tabularChart" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="文章类型" label-width="80px">
					<el-input placeholder="文章类型" v-model="articleData.typeId" autocomplete="off"></el-input>
				</el-form-item>
				<el-form-item label="文章内容" label-width="80px">
					<!-- <el-input placeholder="文章内容" v-model="articleData.details" autocomplete="off"></el-input> -->
					<quill-editor v-model="articleData.details"></quill-editor>
				</el-form-item>
				

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancle">取 消</el-button>
				<el-button type="primary" @click="confirm">确 定</el-button>
			</div>
		</el-dialog>
		<!-- 表格区域 -->
		<el-table :data="articleList" border style="width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="id" label="ID"></el-table-column>
			<el-table-column prop="articleName" label="文章名称"></el-table-column>
			<el-table-column prop="articleIntroduce" label="文章简介"></el-table-column>
			<el-table-column prop="tabularChart" label="列表图"></el-table-column>
			<el-table-column prop="readQuantity" label="阅读数量"></el-table-column>
			<el-table-column prop="collects" label="收藏数量"></el-table-column>
			<el-table-column prop="createTime" label="创建时间"></el-table-column>
			<el-table-column label="操作">
				<template slot-scope='scope'>
				<el-link type="primary" @click="editCommunity(scope.row)">编辑文章</el-link>
				<el-link type="primary">删除文章</el-link>
			</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				// 查询信息对象
				queryInfo: {
					articleName: '',
					pageNum: 1,
					pageSize: 10,
					
					
				},
				title:'',
				dialogFormVisible:false,
				articleData:{
						addOrUpdateFlag :1,
						articleName:'',
						articleIntroduce:'',
						tabularChart:'',
						readQuantity:'',
						collects:'',
						createTime:'',
						details:'',
						typeId:'',
					},
					
				total: 183,
				articleList: []

			}
		},
		mounted() {
			this.queryList();
		},
		methods: {
			// 分页查询信息的方法
			queryList() {
				// 调用后端接口来获取数据
				this.$http.get('article/find', {
						params: this.queryInfo
					})
					.then(resp => {
						if (resp.data.status == 200) {
							console.log(resp.data)
							this.articleList = resp.data.data.list;
							this.total = resp.data.data.total;
							console.log(this.articleList)
						} else {
							this.$message.error('查询失败！')
						}
					})
			},
			addArticle() {
				this.dialogFormVisible = true;
				this.title = "新增文章";
				this.articleData.addOrUpdateFlag = 1;
			},
			editCommunity(row) {
	    		this.title = '编辑文章' //把title改为编辑小区
	    		this.articleData = row
	    		this.dialogFormVisible = true //显示弹出框
	    		this.articleData.addOrUpdateFlag = 2 //把状态码改为2
	    		
},
			//取消
			cancle() {
						this.dialogFormVisible=false;
						this.articleData={
						addOrUpdateFlag :1,
						articleName:'',
						articleIntroduce:'',
						tabularChart:'',
						readQuantity:'',
						collects:'',
						createTime:'',
						details:'',
						typeId:'',	
						}
					},
			//点击确定按钮,弹出成功提示，重新请求数据
			confirm() {
				if(this.articleData.addOrUpdateFlag==1){
					this.$http.post( "article/add",this.articleData
					)
						.then((resp) => {
							//console.log(resp)
							if (resp.data.status == 200) {
								this.$message.success(resp.data.message)
								this.queryList()
								this.cancle()
							} else {
								this.$message.error('操作失败!')
							}
						})
				}else{
					this.$http.put( "article/edit",this.articleData
					)
						.then((resp) => {
							//console.log(resp)
							if (resp.data.status == 200) {
								this.$message.success(resp.data.message)
								this.queryList()
								this.cancle()
							} else {
								this.$message.error('操作失败!')
							}
						})
				}
			},
			handleSizeChange(size) {
				//console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size;
				this.queryInfo.pageNum = 1;
				// 重新查询
				this.queryList()
			},
			handleCurrentChange(num) {
				//console.log('页码发生改变');
				this.queryInfo.pageNum = num;
				// 重新查询
				this.queryList();
			}
		}
	}
</script>
<style>
	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}
</style>